package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.TakeTimeService;
import cn.itcast.bos.web.action.base.web.utils.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class TakeTimeAction extends BaseAction<TakeTime> {

    @Autowired
    private TakeTimeService takeTimeService;

    @Action(value = "takeTime_noAssociationFixedArea" ,results = {@Result(name="success" ,type="json")})
    public String noAssociationFixedArea(){
       List<TakeTime> list =  takeTimeService.findNoAssociationFixedArea();
        ActionContext.getContext().getValueStack().push(list);
        return SUCCESS;
    }

    @Action(value="take_time_pageQuery",results = {@Result(name="success",type="json")})
    public String pageQuery(){
        final Pageable pageable = new PageRequest(page-1,rows);
        final Specification<TakeTime> spec = new Specification<TakeTime>(){
            @Override
            public Predicate toPredicate(Root<TakeTime> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list= new ArrayList<>();
                if(StringUtils.isNotBlank(model.getName())){
                    Predicate name = cb.like(root.get("name").as(String.class), model.getName());
                    list.add(name);
                }
                if(StringUtils.isNotBlank(model.getCompany())){
                    Predicate company = cb.like(root.get("company").as(String.class), model.getCompany());
                    list.add(company);
                }
                if(StringUtils.isNotBlank(model.getOperator())){
                    Predicate operator = cb.like(root.get("operator").as(String.class), model.getName());
                    list.add(operator);
                }

                return cb.and(list.toArray(new Predicate[0]));
            }

        };
        Page<TakeTime> pageData = takeTimeService.findALL(spec,pageable);
        PagePushIntoValueStack(pageData);
        return SUCCESS;
    }

    @Action(value = "take_time_save",results = {@Result(name="success",type = "redirect",location = "./pages/base/take_time.html")})
    public String save(){
        Date operatingTime = model.getOperatingTime();
        System.out.println(operatingTime);
        takeTimeService.save(model);
        return SUCCESS;
    }
}
